home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / c-tools / c_examples / led / window.cpp < prev   
Encoding:
C/C++ Source or Header  |  1996-05-13  |  10.5 KB  |  487 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. // window.cpp
  3. //
  4. // Jeffry A Worth
  5. // November 10, 1995
  6. //////////////////////////////////////////////////////////////////////////////
  7.  
  8. //////////////////////////////////////////////////////////////////////////////
  9. // INCLUDES
  10. #include <string.h>
  11. #include "aframe:include/window.hpp"
  12. #include "aframe:include/gadget.hpp"
  13. #include "aframe:include/rastport.hpp"
  14.  
  15. //////////////////////////////////////////////////////////////////////////////
  16. //
  17.  
  18. AFWindow::AFWindow()
  19.     :m_pWindow(NULL),
  20.     m_pscreen(NULL),
  21.     m_papp(NULL),
  22.     m_statusObject(NULL)
  23. {
  24. }
  25.  
  26. AFWindow::~AFWindow()
  27. {
  28.     // Call Destroy object is not already called
  29.     if(m_pWindow)
  30.         DestroyObject();
  31. }
  32.  
  33. BOOL AFWindow::Create(AFAmigaApp* app, AFRect* rect)
  34. {
  35.   return Create(app,rect,"");
  36. }
  37.  
  38. BOOL AFWindow::Create(AFScreen* screen, AFRect* rect)
  39. {
  40.   return Create(screen,rect,"");
  41. }
  42.  
  43. BOOL AFWindow::Create(AFWindow* window, AFRect* rect)
  44. {
  45.   return Create(window,rect,"");
  46. }
  47.  
  48. BOOL AFWindow::Create(AFWindow* window, AFRect* rect, char* szTitle)
  49. {
  50.   if(!m_pscreen)
  51.     return Create(window->m_papp,rect,szTitle);
  52.   else
  53.     return Create(window->m_pscreen,rect,szTitle);
  54. }
  55.  
  56. BOOL AFWindow::Create(AFAmigaApp* app, AFRect* rect, char *szTitle)
  57. {
  58.   struct WindowRange wr;
  59.  
  60.   // Remember the Application for later
  61.   m_papp = app;
  62.  
  63.   // Create a string for the Window Title
  64.   m_sztitle = new char[strlen(szTitle)+1];
  65.   if(m_sztitle) strcpy(m_sztitle,szTitle);
  66.  
  67.   // Fill in the window range structure;
  68.   SetWindowRange(&wr);
  69.  
  70.   // Open the Window the intuition way
  71.   if(m_pWindow = OpenWindowTags((struct NewWindow*)NULL,
  72.     WA_Left,    rect->TopLeft()->m_x,
  73.     WA_Top,        rect->TopLeft()->m_y,
  74.     WA_Width,    rect->Width(),
  75.     WA_Height,    rect->Height(),
  76.     WA_Title,    m_sztitle,
  77.     WA_MinWidth,    wr.minWidth,
  78.     WA_MinHeight,    wr.minHeight,
  79.     WA_MaxWidth,    wr.maxWidth,
  80.     WA_MaxHeight,    wr.maxHeight,
  81.     WA_IDCMP,    WindowIDCMP(),
  82.     WA_Flags,    WindowFlags(),
  83.     TAG_END)) {
  84.  
  85.     // Fill the textattr structure
  86.     ::AskFont(m_pWindow->RPort,&m_textattr);
  87.  
  88.     m_papp->addWindow(this);
  89.  
  90.     // Call OnCreate Method
  91.     OnCreate();
  92.  
  93.     // Create was successful
  94.     return TRUE;
  95.   }
  96.   return FALSE;
  97. }
  98.  
  99. BOOL AFWindow::Create(AFScreen* screen, AFRect* rect, char *szTitle)
  100. {
  101.   struct WindowRange wr;
  102.  
  103.   // Remember the Application for later
  104.   m_papp = screen->m_papp;
  105.   m_pscreen = screen;
  106.  
  107.   // Create a string for the Window Title
  108.   m_sztitle = new char[strlen(szTitle)+1];
  109.   if(m_sztitle) strcpy(m_sztitle,szTitle);
  110.  
  111.   // Fill in the window range structure;
  112.   SetWindowRange(&wr);
  113.  
  114.   // Open the Window the intuition way
  115.   if(m_pWindow = OpenWindowTags((struct NewWindow*)NULL,
  116.     WA_Left,    rect->TopLeft()->m_x,
  117.     WA_Top,        rect->TopLeft()->m_y,
  118.     WA_Width,    rect->Width(),
  119.     WA_Height,    rect->Height(),
  120.     WA_Title,    m_sztitle,
  121.     WA_MinWidth,    wr.minWidth,
  122.     WA_MinHeight,    wr.minHeight,
  123.     WA_MaxWidth,    wr.maxWidth,
  124.     WA_MaxHeight,    wr.maxHeight,
  125.     WA_IDCMP,    WindowIDCMP(),
  126.     WA_Flags,    WindowFlags(),
  127.     WA_PubScreen,    screen->m_pScreen,
  128.     TAG_END)) {
  129.  
  130.     // Fill the textattr structure
  131.     ::AskFont(m_pWindow->RPort,&m_textattr);
  132.  
  133.     m_papp->addWindow(this);
  134.  
  135.     // Call OnCreate Method
  136.     OnCreate();
  137.  
  138.     // Create was successful
  139.     return TRUE;
  140.   }
  141.   return FALSE;
  142. }
  143.  
  144. ///////////////////////////////////////////////////////////
  145. // GetMsg
  146. //        Gets an IntuiMessage from the window handle and
  147. //    returns it.  This is generally only called by AFAmigaApp
  148. //
  149. LPIntuiMessage
  150. AFWindow::GetMsg()
  151. {
  152.     if(m_pWindow)
  153.         return (LPIntuiMessage)::GetMsg(m_pWindow->UserPort);
  154.     return NULL;
  155. }
  156.  
  157. LPAppMessage
  158. AFWindow::GetAppMsg(LPMsgPort mport)
  159. {
  160.     if(m_pWindow)
  161.         return (LPAppMessage)::GetMsg(mport);
  162.     return NULL;
  163. }
  164.  
  165. void
  166. AFWindow::ReplyMsg(LPIntuiMessage imess)
  167. {
  168.     if(m_pWindow)
  169.         ::ReplyMsg((LPMessage)imess);
  170. }
  171.  
  172. void AFWindow::DestroyWindow()
  173. {
  174.   LPGadget gad;
  175.   AFPtrDlistIterator iter(m_gadgets);
  176.  
  177.   if(m_pWindow) {
  178.  
  179.     // Destroy any cgadget objects that are attached
  180.     // CONVERT THIS TO A PTRDLIST with ITERATOR
  181.     //gad = m_pWindow->FirstGadget;
  182.     //while(gad) {
  183.     //  if(gad->UserData)
  184.     //  ((AFGadget*)gad->UserData)->DestroyObject();
  185.     //  gad=gad->NextGadget;
  186.     //}
  187.  
  188.     // Destroy any afgadgets still attached to the afwindow
  189.     iter.reset();
  190.     while(++iter)
  191.         iter.key()->DestroyObject();
  192.  
  193.     m_papp->removeWindow(this);
  194.  
  195.     CloseWindow(m_pWindow),m_pWindow=NULL;
  196.   }
  197.   if(m_sztitle) delete m_sztitle;
  198. }
  199.  
  200. void
  201. AFWindow::DestroyObject()
  202. {
  203. }
  204.  
  205. void AFWindow::SetWindowRange(LPWindowRange wrange)
  206. {
  207.   wrange->minWidth=50;
  208.   wrange->minHeight=50;
  209.   wrange->maxWidth=~0;
  210.   wrange->maxHeight=~0;
  211. }
  212.  
  213. BOOL AFWindow::ValidPoint(AFPoint* point)
  214. {
  215.   // Check X value
  216.   if( (point->m_x <= m_pWindow->BorderLeft) || (point->m_x >= (m_pWindow->Width-m_pWindow->BorderRight) ) )
  217.     return FALSE;
  218.  
  219.   // Check Y value
  220.   if( (point->m_y <= m_pWindow->BorderTop) || (point->m_y >= (m_pWindow->Height-m_pWindow->BorderBottom) ) )
  221.     return FALSE;
  222.   
  223.   return TRUE;
  224. }
  225.  
  226. void AFWindow::AdjustPoint(AFPoint* point)
  227. {
  228.   // Adjust X value
  229.   if(point->m_x <= m_pWindow->BorderLeft)
  230.     point->m_x = m_pWindow->BorderLeft;
  231.   else if(point->m_x >= (m_pWindow->Width-m_pWindow->BorderRight) )
  232.     point->m_x = m_pWindow->Width - m_pWindow->BorderRight - 1;
  233.  
  234.   // Adjust Y value
  235.   if(point->m_y <= m_pWindow->BorderTop)
  236.     point->m_y = m_pWindow->BorderTop;
  237.   else if(point->m_y >= (m_pWindow->Height-m_pWindow->BorderBottom) )
  238.     point->m_y = m_pWindow->Height-m_pWindow->BorderBottom-1;
  239. }
  240.  
  241. void AFWindow::GetDisplayRect(AFRect* rect)
  242. {
  243.   rect->SetRect(m_pWindow->BorderLeft,m_pWindow->BorderTop,
  244.     m_pWindow->Width-m_pWindow->BorderRight-1,m_pWindow->Height-m_pWindow->BorderBottom-1);
  245. }
  246.  
  247. // Intuition Events
  248.  
  249. void AFWindow::OnNewSize(LPIntuiMessage imess)
  250. {
  251.     AFPtrDlistIterator iter(m_gadgets);
  252.  
  253.     iter.reset();
  254.     while(++iter)
  255.         ((AFGadget*)iter.key())->OnPaint();
  256. }
  257.  
  258. void AFWindow::OnGadgetUp(LPIntuiMessage imess)
  259. {
  260.   AFGadget *ptr;
  261.   char string[10];
  262.  
  263.   ptr = (AFGadget*)GadgetFromID(((LPGadget)imess->IAddress)->GadgetID);
  264.   sprintf(string,"%d",((LPGadget)imess->IAddress)->GadgetID);
  265.   printf("%s\n",string);
  266.   ptr->OnGadgetUp(imess);
  267. }
  268.  
  269. void AFWindow::OnGadgetDown(LPIntuiMessage imess)
  270. {
  271.   AFGadget *ptr;
  272.  
  273.   //ptr = (AFGadget*)GadgetFromID(((LPGadget)imess->IAddress)->GadgetID);
  274.   //ptr->OnGadgetDown(imess);
  275. }
  276.  
  277. void AFWindow::OnCloseWindow(LPIntuiMessage imess)
  278. {
  279.   DestroyWindow();
  280. }
  281.  
  282. void AFWindow::OnGadgetHelp(LPIntuiMessage imess)
  283. {
  284.     if(m_statusObject) {
  285.         if(imess->IAddress == m_pWindow)
  286.             m_statusObject->SetText(m_statusText.data());
  287.         else if(imess->IAddress) {
  288.             AFGadget* gadget;
  289.             LONG sysgad=((LPGadget)imess->IAddress)->GadgetType & 0xF0;
  290.             //printf("%x\n",sysgad);
  291.             //switch(sysgad) {
  292.             //case GTYP_SIZING:
  293.             //    m_statusObject->SetText(m_sizeStatusText.data()); break;
  294.             //case GTYP_WDRAGGING:
  295.             //    m_statusObject->SetText(m_dragStatusText.data()); break;
  296.             //case GTYP_WUPFRONT:
  297.             //    m_statusObject->SetText(m_depthStatusText.data()); break;
  298.             //case GTYP_WDOWNBACK:
  299.             //    m_statusObject->SetText(m_zoomStatusText.data()); break;
  300.             //case GTYP_CLOSE:
  301.             //    m_statusObject->SetText(m_closeStatusText.data()); break;
  302.             //default:
  303.             if (sysgad) 
  304.                 m_statusObject->SetText("");
  305.             else
  306.                 if(((LPGadget)imess->IAddress)->UserData) {
  307.                     gadget = (AFGadget*)((LPGadget)imess->IAddress)->UserData;
  308.                     m_statusObject->SetText(gadget->m_statusText.data());
  309.                 } else
  310.                     m_statusObject->SetText("");
  311.             //    break;
  312.             //}
  313.         } else
  314.             m_statusObject->SetText("");
  315.     }
  316. }
  317.  
  318. // Intuition Functions
  319.  
  320. void AFWindow::SetWindowTitles(UBYTE* lpszWindowTitle, UBYTE* lpszScreenTitle)
  321. {
  322.   ::SetWindowTitles(m_pWindow,lpszWindowTitle,lpszScreenTitle);
  323. }
  324.  
  325. void AFWindow::SizeWindow(WORD deltax, WORD deltay)
  326. {
  327.   ::SizeWindow(m_pWindow, deltax, deltay);
  328. }
  329.  
  330. void AFWindow::WindowToBack()
  331. {
  332.   ::WindowToBack(m_pWindow);
  333. }
  334.  
  335. void AFWindow::WindowToFront()
  336. {
  337.   ::WindowToFront(m_pWindow);
  338. }
  339.  
  340. void AFWindow::ZipWindow()
  341. {
  342.   ::ZipWindow(m_pWindow);
  343. }
  344.  
  345. void AFWindow::RefreshGadgets()
  346. {
  347.     AFPtrDlistIterator iter(m_gadgets);
  348.  
  349.     ::RefreshGadgets(m_pWindow->FirstGadget,m_pWindow, (struct Requester*)NULL);
  350.  
  351.     iter.reset();
  352.     while(++iter)
  353.         ((AFGadget*)iter.key())->OnPaint();
  354. }
  355.  
  356. void
  357. AFWindow::Clear(UBYTE pen)
  358. {
  359.   AFRastPort rp(this);
  360.   AFRect rect;
  361.  
  362.   rp.SetAPen(pen);
  363.   GetDisplayRect(&rect);
  364.   rp.RectFill(&rect);
  365. }
  366.  
  367. void
  368. AFWindow::ExecuteMsg(LPIntuiMessage imess)
  369. {
  370.     switch(imess->Class) {
  371.     case IDCMP_SIZEVERIFY:
  372.         OnSizeVerify(imess); break;
  373.     case IDCMP_NEWSIZE:
  374.         OnNewSize(imess); break;
  375.     case IDCMP_REFRESHWINDOW:
  376.         OnRefreshWindow(imess); break;
  377.     case IDCMP_MOUSEBUTTONS:
  378.         OnMouseButtons(imess); break;
  379.     case IDCMP_MOUSEMOVE:
  380.         OnMouseMove(imess); break;
  381.     case IDCMP_GADGETDOWN:
  382.         OnGadgetDown(imess); break;
  383.     case IDCMP_GADGETUP:
  384.         OnGadgetUp(imess); break;
  385.     case IDCMP_REQSET:
  386.         OnReqSet(imess); break;
  387.     case IDCMP_MENUPICK:
  388.         OnMenuPick(imess); break;
  389.     case IDCMP_CLOSEWINDOW:
  390.         OnCloseWindow(imess); break;
  391.     case IDCMP_RAWKEY:
  392.         OnRawKey(imess); break;
  393.     case IDCMP_REQVERIFY:
  394.         OnReqVerify(imess); break;
  395.     case IDCMP_REQCLEAR:
  396.         OnReqClear(imess); break;
  397.     case IDCMP_MENUVERIFY:
  398.         OnMenuVerify(imess); break;
  399.     case IDCMP_NEWPREFS:
  400.         OnNewPrefs(imess); break;
  401.     case IDCMP_DISKINSERTED:
  402.         OnDiskInserted(imess); break;
  403.     case IDCMP_DISKREMOVED:
  404.         OnDiskRemoved(imess); break;
  405.     case IDCMP_WBENCHMESSAGE:
  406.         OnWBenchMessage(imess); break;
  407.     case IDCMP_ACTIVEWINDOW:
  408.         OnActiveWindow(imess); break;
  409.     case IDCMP_INACTIVEWINDOW:
  410.         OnInActiveWindow(imess); break;
  411.     case IDCMP_DELTAMOVE:
  412.         OnDeltaMove(imess); break;
  413.     case IDCMP_VANILLAKEY:
  414.         OnVanillaKey(imess); break;
  415.     case IDCMP_INTUITICKS:
  416.         OnIntuiTicks(imess); break;
  417.     case IDCMP_IDCMPUPDATE:
  418.         OnIDCMPUpdate(imess); break;
  419.     case IDCMP_MENUHELP:
  420.         OnMenuHelp(imess); break;
  421.     case IDCMP_CHANGEWINDOW:
  422.         OnChangeWindow(imess); break;
  423.     case IDCMP_GADGETHELP:
  424.         OnGadgetHelp(imess); break;
  425.     default:
  426.         OnFutureIDCMP(imess); break;
  427.     }
  428. }
  429.  
  430. void
  431. AFWindow::ExecuteAppMsg(LPAppMessage amess)
  432. {
  433.     switch(amess->am_Type) {
  434.     case AMTYPE_APPWINDOW:
  435.         OnAppWindow(amess); break;
  436.     case AMTYPE_APPICON:
  437.         OnAppIcon(amess); break;
  438.     case AMTYPE_APPMENUITEM:
  439.         OnAppMenu(amess); break;
  440.     default:
  441.         OnFutureIDCMP(amess); break;
  442.     }
  443. }
  444.  
  445. BOOL
  446. AFWindow::isValid()
  447. {
  448.     return(m_pWindow!=NULL);
  449. }
  450.  
  451. void
  452. AFWindow::SetStatusPanel(AFObject* obj)
  453. {
  454.     m_statusObject = obj;
  455. }
  456.  
  457. void
  458. AFWindow::HelpControl(ULONG flags)
  459. {
  460.     ::HelpControl(m_pWindow,flags);
  461. }
  462.  
  463. BOOL
  464. AFWindow::AppendGadget(AFObject* gadget)
  465. {
  466.     return m_gadgets.append(gadget);
  467. }
  468.  
  469. void
  470. AFWindow::RemoveGadget(AFObject* gadget, BOOL deleteobject)
  471. {
  472.     // Not supported yet.
  473. }
  474.  
  475. AFObject*
  476. AFWindow::GadgetFromID(ULONG id)
  477. {
  478.     AFPtrDlistIterator iter(m_gadgets);
  479.     AFGadget *gad;
  480.  
  481.     iter.reset();
  482.     while(++iter) {
  483.         if( ((AFGadget*)iter.key())->GadgetID()==id)
  484.             return iter.key();
  485.     }
  486. }
  487.